<?php
/**
 * Created by PhpStorm.
 * User: zhaoq02
 * Date: 2015-05-07
 * Time: 17:54
 */

namespace common\utils;

/**
 * Class DateTimeUtil
 * @package common\utils
 */
class DateTimeUtils
{
    /**
     * 获取当前时间
     * @return string
     */
    public static function now()
    {
        date_default_timezone_set("PRC");
        return date('Y-m-d H:i:s');
    }

    /**
     * 获取近n年的日期
     * @return string
     */
    public static function lastYear($n = 1)
    {
        date_default_timezone_set("PRC");
        $currentDate = date('Y-m-d');
        $yearAgo = date('Y-m-d', strtotime('-' . $n . ' year', strtotime($currentDate)));
        return $yearAgo;
    }

    /**
     * 获取当前时间
     * @return string
     */
    public static function time()
    {
        date_default_timezone_set("PRC");
        return time();
    }

    /**
     * 获取当前日期
     * @param string $format
     * @return string
     */
    public static function date($format = 'Y-m-d')
    {
        date_default_timezone_set("PRC");
        return date($format);
    }

    /**
     * 转换时间
     * @param $time
     * @param string $format
     * @return false|string
     */
    public static function convertDate($time, $format = 'Y-m-d H:i:s')
    {
        date_default_timezone_set("PRC");
        return date($format, $time);
    }

    /**
     * @param $date
     * @param $minutes
     * @return DateTime
     */
    public static function AddMinutes($date, $minutes)
    {
        $interval = new \DateInterval('PT' . $minutes . 'M');
        $datetime = new \DateTime($date);
        return date_add($datetime, $interval);
    }


    /**
     * @param $date DateTime|string
     * @return string
     */
    public static function ConvertToString($date, $format = 'Y-m-d H:i:s')
    {
        $returnStr = "";
        try {
            if (!empty($date)) {
                if (is_string($date)) {
                    $dateTime = new \DateTime($date);
                    $returnStr = date_format($dateTime, $format);
                } else {
                    $returnStr = date_format($date, $format);
                }
            }
        } catch (\Exception $ex) {

        }
        return $returnStr;
    }

    /**
     * 把时间转换为GMT标准时间
     * @param $date
     * @return string
     */
    public static function convertToGmtTime($date)
    {
        return gmdate("Y-m-d\TH:i:s\Z", strtotime($date));
    }

    public static function formatDate($time)
    {
        date_default_timezone_set("PRC");
        if (is_string($time)) {
            $time = strtotime($time);
        }
        //获取今天凌晨的时间戳
        $day = strtotime(date('Y-m-d', time()));
        //获取昨天凌晨的时间戳
        $pday = strtotime(date('Y-m-d', strtotime('-1 day')));
        //获取现在的时间戳
        $nowtime = time();
        $tc = $nowtime - $time;
        if ($time < $pday) {
            if ($time > strtotime(date('Y-m-d', strtotime('-3 day')))) {
                $str = "三天内";
            } else if ($time > strtotime(date('Y-m-d', strtotime('-7 day')))) {
                $str = "一周内";
            } else if ($time > strtotime(date('Y-m-d', strtotime('-30 day')))) {
                $str = '一个月内';
            } else {
                $str = '一个月前';//date('Y-m-d', $time);
            }
        } elseif ($time < $day && $time > $pday) {
            $str = "昨天";
        } elseif ($tc > 60 * 60) {
            $str = floor($tc / (60 * 60)) . "小时前";
        } elseif ($tc > 60) {
            $str = floor($tc / 60) . "分钟前";
        } else {
            $str = "一分钟内";
        }
        return $str;
    }

    /**
     * 计算时间差 返回天数(参数时间戳)
     * @param $current_time
     * @param $last_time
     * @return int
     */
    public static function getTimeDiff($current_time, $last_time)
    {
        $current_time_str = strtotime(date("Ymd", $current_time));
        $last_time_str = strtotime(date("Ymd", $last_time));
        if ($current_time_str >= $last_time_str) {
            $timediff = $current_time_str - $last_time_str;
            $days = intval($timediff / 86400);
        } else {
            $days = 0;
        }
        return $days;
    }
}
